package com.ruoyi.zsyh.controller;

import java.util.List;

import com.ruoyi.common.core.domain.ZtreeString;
import com.ruoyi.common.utils.EmptyUtils;
import com.ruoyi.common.utils.IdWorker;
import com.ruoyi.common.utils.PingYinUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.zsyh.domain.ZsyhPlatformPartstype;
import com.ruoyi.zsyh.service.IZsyhPlatformPartstypeService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.domain.Ztree;

/**
 * 产品分类Controller
 * 
 * @author yuexl
 * @date 2021-12-08
 */
@Controller
@RequestMapping("/zsyh/partstype")
public class ZsyhPlatformPartstypeController extends BaseController
{
    private String prefix = "zsyh/partstype";

    @Autowired
    private IZsyhPlatformPartstypeService zsyhPlatformPartstypeService;

    @RequiresPermissions("zsyh:partstype:view")
    @GetMapping()
    public String partstype()
    {
        return prefix + "/partstype";
    }

    /**
     * 查询产品分类树列表
     */
    @RequiresPermissions("zsyh:partstype:list")
    @PostMapping("/list")
    @ResponseBody
    public List<ZsyhPlatformPartstype> list(ZsyhPlatformPartstype zsyhPlatformPartstype)
    {
        List<ZsyhPlatformPartstype> list = zsyhPlatformPartstypeService.selectZsyhPlatformPartstypeList(zsyhPlatformPartstype);
        return list;
    }

    /**
     * 导出产品分类列表
     */
    @RequiresPermissions("zsyh:partstype:export")
    @Log(title = "产品分类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ZsyhPlatformPartstype zsyhPlatformPartstype)
    {
        List<ZsyhPlatformPartstype> list = zsyhPlatformPartstypeService.selectZsyhPlatformPartstypeList(zsyhPlatformPartstype);
        ExcelUtil<ZsyhPlatformPartstype> util = new ExcelUtil<ZsyhPlatformPartstype>(ZsyhPlatformPartstype.class);
        return util.exportExcel(list, "产品分类数据");
    }

    /**
     * 新增产品分类
     */
    @GetMapping(value = { "/add/{id}", "/add/" })
    public String add(@PathVariable(value = "id", required = false) String id, ModelMap mmap)
    {
        if (StringUtils.isNotNull(id))
        {
            mmap.put("zsyhPlatformPartstype", zsyhPlatformPartstypeService.selectZsyhPlatformPartstypeById(id));
        }
        return prefix + "/add";
    }

    /**
     * 新增产品分类
     */
    @GetMapping(value = { "/addNext/{id}", "/addNext/" })
    public String addNext(@PathVariable(value = "id", required = false) String id, ModelMap mmap)
    {
        if (StringUtils.isNotNull(id))
        {
            mmap.put("zsyhPlatformPartstype", zsyhPlatformPartstypeService.selectZsyhPlatformPartstypeById(id));
        }
        return prefix + "/addNext";
    }

    /**
     * 新增保存产品分类
     */
    @RequiresPermissions("zsyh:partstype:add")
    @Log(title = "产品分类", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ZsyhPlatformPartstype zsyhPlatformPartstype)
    {
        zsyhPlatformPartstype.setId(IdWorker.getId());
        if(EmptyUtils.isEmpty(zsyhPlatformPartstype.getpId())){
            zsyhPlatformPartstype.setpId("0");
            zsyhPlatformPartstype.setTypeLevel("1");
        }else{
            zsyhPlatformPartstype.setTypeLevel("2");
        }
        //设置类别代码type_(中文拼音缩写)
        zsyhPlatformPartstype.setTypeCode("type_"+ PingYinUtil.getFirstSpell(zsyhPlatformPartstype.getTypeName()));
        //判断名字是否重复
        ZsyhPlatformPartstype zsyhPlatformPartstype1 = new ZsyhPlatformPartstype();
        zsyhPlatformPartstype1.setTypeName(zsyhPlatformPartstype.getTypeName());
        zsyhPlatformPartstype1.setpId(zsyhPlatformPartstype.getpId());
        if(zsyhPlatformPartstypeService.selectZsyhPlatformPartstypeList(zsyhPlatformPartstype1).size()>0){
            return error("已经存在该分类，不能重复添加");
        }
        return toAjax(zsyhPlatformPartstypeService.insertZsyhPlatformPartstype(zsyhPlatformPartstype));
    }

    /**
     * 修改产品分类
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        ZsyhPlatformPartstype zsyhPlatformPartstype = zsyhPlatformPartstypeService.selectZsyhPlatformPartstypeById(id);
        mmap.put("zsyhPlatformPartstype", zsyhPlatformPartstype);
        return prefix + "/edit";
    }

    /**
     * 修改产品分类
     */
    @GetMapping("/editNext/{id}")
    public String editNext(@PathVariable("id") String id, ModelMap mmap)
    {
        ZsyhPlatformPartstype zsyhPlatformPartstype = zsyhPlatformPartstypeService.selectZsyhPlatformPartstypeById(id);
        mmap.put("zsyhPlatformPartstype", zsyhPlatformPartstype);
        return prefix + "/editNext";
    }

    /**
     * 修改保存产品分类
     */
    @RequiresPermissions("zsyh:partstype:edit")
    @Log(title = "产品分类", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ZsyhPlatformPartstype zsyhPlatformPartstype)
    {
        if("0".equals(zsyhPlatformPartstype.getpId()) || EmptyUtils.isEmpty(zsyhPlatformPartstype.getpId())){
            //如果是一级分类，则修改类型等级为1
            zsyhPlatformPartstype.setTypeLevel("1");
            zsyhPlatformPartstype.setParentIds("0,");
        }else{
            zsyhPlatformPartstype.setTypeLevel("2");
            zsyhPlatformPartstype.setParentIds("0,"+zsyhPlatformPartstype.getpId());
        }
        zsyhPlatformPartstype.setTypeCode("type_"+PingYinUtil.getFirstSpell(zsyhPlatformPartstype.getTypeName()));
        return toAjax(zsyhPlatformPartstypeService.updateZsyhPlatformPartstype(zsyhPlatformPartstype));
    }

    /**
     * 删除
     */
    @RequiresPermissions("zsyh:partstype:remove")
    @Log(title = "产品分类", businessType = BusinessType.DELETE)
    @GetMapping("/remove/{id}")
    @ResponseBody
    public AjaxResult remove(@PathVariable("id") String id)
    {
        return toAjax(zsyhPlatformPartstypeService.deleteZsyhPlatformPartstypeById(id));
    }

    /**
     * 选择产品分类树
     */
    @GetMapping(value = { "/selectPartstypeTree/{id}", "/selectPartstypeTree/" })
    public String selectPartstypeTree(@PathVariable(value = "id", required = false) String id, ModelMap mmap)
    {
        if (StringUtils.isNotNull(id))
        {
            mmap.put("zsyhPlatformPartstype", zsyhPlatformPartstypeService.selectZsyhPlatformPartstypeById(id));
        }
        return prefix + "/tree";
    }

    /**
     * 选择产品分类树
     */
    @GetMapping(value = { "/selectTypeTree/{id}", "/selectTypeTree/" })
    public String selectTypeTree(@PathVariable(value = "id", required = false) String id, ModelMap mmap)
    {
        if (StringUtils.isNotNull(id))
        {
            mmap.put("zsyhPlatformPartstype", zsyhPlatformPartstypeService.selectZsyhPlatformPartstypeById(id));
        }
        return prefix + "/treeAll";
    }

    /**
     * 加载产品分类树列表
     */
    @GetMapping("/treeData")
    @ResponseBody
    public List<ZtreeString> treeData()
    {
        List<ZtreeString> ztrees = zsyhPlatformPartstypeService.selectZsyhPlatformPartstypeTree();
        return ztrees;
    }

    /**
     * 加载产品分类树一级列表
     */
    @GetMapping("/treeDataFirst")
    @ResponseBody
    public List<ZtreeString> treeDataFirst()
    {
        List<ZtreeString> ztrees = zsyhPlatformPartstypeService.selectZsyhPlatformPartstypeTreeFirst();
        return ztrees;
    }
}
